/*
* @Author: Administrator
* @Date:   2017-11-03 15:38:09
* @Last Modified by:   Administrator
* @Last Modified time: 2017-12-01 11:15:01
*/
 
 $(function(){

    $("footer").load("layout/footer.html");
    $("header").load("layout/header2.html");

 var listHtml=$('#list').html();
 var navHtml=$('#nav').html();

 $(function() {
              $.ajax({
                       type:"get",
                       url:"data/zylistnav.json",
                       async:true,
                       success: function(data){
                        console.log(data.nav.length);
                        var navdata=data.nav
                        $('.navlis').html(doT.template(navHtml)(navdata));
                       },
                       error: function(XMLHTTPRequest, textStatus, errorThrown){
                         console.log("XMLHttpRequest :"+ XMLHTTPRequest);
                          console.log("textStatus :"+textStatus);
                         console.log("errorThrown :"+errorThrown);
                       }
                     });
             
       
    });
// 导航
$(document).on("click",".navlis button",function(){
  $(this).addClass("changered").siblings().removeClass("changered");
})
window.onload=function(){
  console.log($(".navlis li:nth-child(1) .casebtn button").length)
}

// 导航分类切换样式
         $(document).on("mouseenter",".navlis li:nth-child(1) .casebtn button",function(){
         // 设index为当前点击
         var index = $(this).index();
         // 点击添加样式利用siblings清除其他兄弟节点样式
         $(this).addClass("bgred").siblings().removeClass("bgred");
         // 同理显示与隐藏
         $(this).parents(".casebtn").siblings(".casebtn-small").find(".smallcase-box").eq(index).show().siblings().hide();
          if($(".smallcase-box:eq("+index+") button").length<=12){
                $(".smallcase-box:eq("+index+") button").parents(".casebtn-small").css("height","45")
          }else{
            $(".smallcase-box:eq("+index+") button").parents(".casebtn-small").css("height","90")
          }      
 
       });

// 更多
$(document).on("click",".morecity",function(){
  if($(".list-nav li:nth-child(2)").height()==50){
      $(".list-nav li:nth-child(2)").height(160)
  }else{
    $(".list-nav li:nth-child(2)").height(50)
  }
})
// 排序
$(document).on("click",".sort-item",function(){
  $(this).addClass("changered").siblings().removeClass("changered");
})
window.item=false
// 布局方式
$(document).on("click",".sort-way:eq(0)",function(){
  $(this).addClass("changebg").siblings().removeClass("changebg");
  $(".p-list-con").addClass("item")
  window.item=true;
})

$(document).on("click",".sort-way:eq(1)",function(){
  $(this).addClass("changebg").siblings().removeClass("changebg");
  $(".p-list-con").removeClass("item")
  window.item=false;

})
console.log(window.item)
// 选中内容增添删除样式
          $(document).on("click",".navlis li:nth-child(1) .casebtn-small .smallcase-box button",function(){
              $(this).addClass("bgred").siblings().removeClass("bgred");
              var html=$(this).html()
              $(".zylist-del-check ul .zylist-delli:nth-child(1)").show().find("span").html(html)
          })
          $(document).on("click",".navlis li:nth-child(2) .casebtn button",function(){
              $(this).addClass("changered").siblings().removeClass("changered");
              var html=$(this).html()
              $(".zylist-del-check ul .zylist-delli:nth-child(2)").show().find("span").html(html)
          })

          $(document).on("click",".clear-check",function(){
                $(".zylist-del-check ul li").hide();
                $(".casebtn button").removeClass("changered");
                
          });

        
        // 删除选择按钮
        $(document).on("click",".zylist-delli:nth-child(1) .zylist-del-btn",function(){
           $(this).parents(".zylist-delli").hide();
           $(".navlis li:nth-child(1) .casebtn-small .smallcase-box button").removeClass("changered");

        })
        $(document).on("click",".zylist-delli:nth-child(2) .zylist-del-btn",function(){
           $(this).parent(".zylist-delli:nth-child(2)").hide();
           $(".navlis li:nth-child(2) button").removeClass("changered");
        })
      


    //分页 
    function ajaxTest(num) {
      $.ajax({
        url: "data/zylist.json",
        type: "get",
        data: {},
        dataType: "json",
        success: function(data) {
           var listdata=data.list;
           console.log(listdata)
           $('.p-list-con').html(doT.template(listHtml)(listdata));
           $(".list-lis-detail .list-lis-price .price-box:last-child").css("border","0")
           // 根据数量大小确定单位
            for(var i=0;i<$(".list-lis-detail .list-lis-price").length;i++){
                if($(".list-lis-detail .list-lis-price:eq("+i+") .price-box").length==2){
                 $(".list-lis-detail .list-lis-price:eq("+i+") .price-box:eq(0) span.unit").append("<i>件以下</i>")
                 $(".list-lis-detail .list-lis-price:eq("+i+") .price-box:eq(1) span.unit").append("<i>件</i>")
                 $(".list-lis-detail .list-lis-price:eq("+i+") .price-box").css("width","128")
               }else if($(".list-lis-detail .list-lis-price:eq("+i+") .price-box").length==3){
                  $(".list-lis-detail .list-lis-price:eq("+i+") .price-box:eq(0) span.unit").append("<i>件以下</i>")
                 $(".list-lis-detail .list-lis-price:eq("+i+") .price-box:eq(1) span.unit").append("<i>件</i>")
                 $(".list-lis-detail .list-lis-price:eq("+i+") .price-box:eq(2) span.unit").append("<i>件以上</i>")
                 $(".list-lis-detail .list-lis-price:eq("+i+") .price-box").css("width","80")
               }else{
                  $(".list-lis-detail .list-lis-price:eq("+i+") .price-box:eq(0) span.unit").html("")
                 $(".list-lis-detail .list-lis-price:eq("+i+") .price-box").css({
                   "width":"100%",
                   "text-align":"left",
                   "line-height":"55px",
                   "height":"55"
                 })

               }
            }

             console.log($(".list-lis-detail .list-lis-price .price-box").length)
            
           $("html,body").animate({
              "scrollTop" : 0
            },400);
          //分页
          $("#page").paging({
            pageNo: num,
            totalPage: data.totalPage,
            totalSize: data.totalSize,
            callback: function(num) {
              console.log(typeof data)
              ajaxTest(num)
            }
          })
        }
      })
    }

   ajaxTest(1);
})
